package com.elite.groovy.callback


import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONArray
import org.json.JSONObject
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @projectName: Groovy
 * @package: com.elite.groovy.callback
 * @className: apiDynSql
 * @description: 信创DYNSQL表验证
 * @author: Rick
 * @date: 2024-08-16 11:13
 * @version: 1.0
 */

public class apiDyn_zj implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiDyn_zj.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        long startTime = System.currentTimeMillis();
        String Check_sql = "", SqlType = "", CheckStr = "", DYNID = "", DYNID2 = "", DYNNAME = "", StrDynSqlJSON = "";
        int Check_Status = 0;

        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        //扫描动态页面
        String DynSql = '''select t.DYNID,DYNID2,DYNNAME,tp.content as DynSqlInfo from  tab_dyn t 
                          left join tab_dyn_page tp on t.dynid=tp.dynid
                           WHERE t.dyntype=0 '''

        //清理数据
        jt.update("delete from xc_dyn_zujian");
        List<Map<String, Object>> PustSqlList = jt.queryForList(DynSql);

        for (int i = 0; i < PustSqlList.size(); i++) {
            try {
                DYNID = PustSqlList.get(i).get("DYNID");  //36位动态页面ID
                DYNID2 = PustSqlList.get(i).get("DYNID2");//6位动态页面ID
                DYNNAME = PustSqlList.get(i).get("DYNNAME");
                StrDynSqlJSON = PustSqlList.get(i).get("DynSqlInfo");

                log.debug("正在处理动态页面第" + i + "个：" + DYNID);
                //jSON遍历解析

                JSONObject obj = new JSONObject(StrDynSqlJSON.toString());

                traversJson(jt, obj, DYNID, DYNID2, DYNNAME);
            } catch (Exception e) {
            }

        }


    }

    private static void traversJson(jt, JSONObject obj, DYNID, DYNID2, DYNNAME) {
        Iterator<String> keys = obj.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            Object value = obj.get(key);
            if (value.toString().length() == 36) {
                log.debug("key值:" + key);
                log.debug("value值:" + value);
                String zjsql = ''' select DYNID2,DYNNAME from tab_dyn where dynid=? and dyntype=1 '''
                List zjcount = jt.queryForList(zjsql, value);
                if (zjcount.size() > 0) {
                    String zjdynid2 = zjcount.get(0).get("DYNID2");
                    String zjdynname = zjcount.get(0).get("DYNNAME");
                    String UUID = UUID.randomUUID().toString();
                    String zjtable = ''' insert into xc_dyn_zujian(rec_guid,dynguid,dynid,dynname,zj_dynguid,zj_dynid,zj_dynname)
                                        values(?,?,?,?,?,?,?)
                                         '''
                    jt.update(zjtable, UUID, DYNID, DYNID2, DYNNAME, value, zjdynid2, zjdynname);
                }

            }

            if (value instanceof JSONObject) {
                traversJson(jt, (JSONObject) value, DYNID, DYNID2, DYNNAME);
            }
            try {
                JSONArray valuearr = new JSONArray(value)
                for (int i = 0; i < valuearr.size(); i++) {
                    traversJson(jt, (JSONObject) valuearr[i], DYNID, DYNID2, DYNNAME);
                }
            } catch (Exception e) {
            }
        }
    }

}




